<template>
	<!-- quan_type "0" 满减券 quan_type "1" 商品兑换券 -->
	<view class="coupon-item-wrap">
		<view class="images-inner">
			<image class="image" :src="couponImage" mode="aspectFill"></image>
		</view>
		<view class="discount-coupon-wrap">
			<template v-if="isDiscountCoupon">
				<view class="left_price_wrap">
					<view class="discount_coupon_price">
						<text class="price_value">{{ record.coupon }}</text>
						<text class="price_prefix">￥</text>
					</view>
					<view class="discount_coupon_price_footer">
						<text>-</text>
						<text>DISCOUNT</text>
						<text>-</text>
					</view>
				</view>
				<view class="discount_center_wrap">
					<text>满{{ record.full_deduction }}元可用</text>
					<text class="discount_coupon_expire_time">有效期至{{ couponEndDate }}</text>
				</view>
			</template>
			<template v-else>
				<div class="left_price_wrap">
					<text class="name_text">{{ record.title }}</text>
					<text class="name_desc">详情请咨询客服</text>
				</div>
				<view class="discount_center_wrap">
					<div class="goods_value_wrap">
						<text>价值</text>
						<text class="goods_value_prefix">￥</text>
						<text class="goods_value_text">{{ record.coupon }}元</text>
					</div>
					<text class="discount_coupon_expire_time">有效期至{{ couponEndDate }}</text>
				</view>
				<!-- <div class="coupon_name">
				<text>{{ record.title }}</text>
			</div>
			<div class="coupon_money">
				<text>{{ record.coupon }}</text>
			</div>
			<div class="coupon_btn" v-if="type === 0" @click="getCoupon">
				<text>{{ record.is_ok ? '已领取' : '立即领取' }}</text>
			</div>
			<div class="coupon_btn" v-if="type === 1" @click="getCoupon">
				<text>{{ record.status <= 0 ? '未使用' : '已使用' }}</text>
			</div> -->
			</template>
		</view>
		<div class="coupon_line"></div>
		<div class="coupon_item_btn" v-if="type === 0" @click="getCoupon">
			<text>{{ record.is_ok ? '已领取' : '立即领取' }}</text>
		</div>
		<div class="coupon_item_btn" v-if="type === 1" @click="getCoupon">
			<text>{{ record.status <= 0 ? '未使用' : '已使用' }}</text>
		</div>
	</view>
</template>

<script>
	import { patchZero } from "@/utils/tool.js"
	export default {
		props: ['type', 'record'],
		methods: {
			formatDate(timestamp) {
				const date = new Date(timestamp)
				const [year, month, day] = [date.getFullYear(), date.getMonth() + 1, date.getDate()]
				return `${year}-${patchZero(month)}-${patchZero(day)}`
			},
			getCoupon() {
				if (this.type === 1) return
				if (this.record.is_ok) return
				uni.showModal({
					title: '提示',
					content: '您确定领取吗?',
					success: async ev => {
						if (ev.cancel) return
						const { id } = this.record
						const resp = await this.$request({
							url: "/DiscountCoupon/receiveCoupon",
							data: { id },
						})
						const { code, msg } = resp
						this.$toast({ title: msg })
						if (code !== 1) return
						this.$emit('success', true)
						uni.$emit("updateMyList")
					}
				})

			},
		},
		computed: {
			couponType() {
				return Number(this.record.quan_type)
			},
			couponEndDate() {
				const { indate, end_time } = this.record
				if (this.type === 0) {
					if (!indate) return ""
					return this.formatDate(indate)
				}
				if (!end_time) return ""
				return this.formatDate(end_time)
			},
			couponImage() {
				// const { quan_type } = this.record
				// const map = { 0: "1", 1: "2" }
				return `${this.sourceUrl}/coupon.jpg`
			},
			isDiscountCoupon() {
				const { quan_type } = this.record
				return Number(quan_type) <= 0
			}
		},
		data() {
			return {}
		}
	}
</script>

<style lang="scss" scoped>
	.coupon-item-wrap {
		padding: 28rpx 32rpx;
		box-sizing: border-box;
		position: relative;
		display: flex;
		align-items: center;
		background-color: transparent;
		border-radius: 2rpx;
		overflow: hidden;
		height: 210rpx;
		--circle-left: 230rpx;

		.coupon_line {
			position: absolute;
			left: calc(var(--circle-left) + calc(28rpx / 2));
			top: 0;
			bottom: 0;
			width: 0;
			border-right: 1rpx dashed #fff;
			opacity: .8;
			transform: scale(.7);
		}

		&::before,
		&::after {
			content: "";
			display: block;
			width: 28rpx;
			height: 28rpx;
			background: #F7F8FA;
			border-radius: 50%;
			position: absolute;
			z-index: 3;
			top: 0;
			transform: translateY(-50%);
			left: var(--circle-left);
		}

		&::after {
			top: unset;
			bottom: 0;
			transform: translateY(50%);
		}

		.coupon_item_btn {
			position: absolute;
			right: 40rpx;
			top: 50%;
			transform: translateY(-50%);
			width: 120rpx;
			height: 38rpx;
			background: #FFFFFF;
			border-radius: 4rpx 4rpx 4rpx 4rpx;
			font-size: 20rpx;
			color: #262626;
			display: flex;
			align-items: center;
			justify-content: center;
			line-height: 1;
		}



		.discount-coupon-wrap {
			position: absolute;
			left: 0;
			top: 0;
			right: 0;
			bottom: 0;
			display: flex;
			flex-flow: row nowrap;
			--left-width: 250rpx;
			--center-width: 320rpx;
			--center-padding-left: 24rpx;

			.goods_coupon_name {}

			.discount_center_wrap {
				width: fit-content;
				height: 100%;
				box-sizing: border-box;
				color: #fff;
				font-size: 32rpx;
				display: flex;
				flex-flow: column;
				justify-content: center;
				text-align: center;
				padding-left: var(--center-padding-left);
				font-weight: normal;

				.goods_value_wrap {
					display: flex;
					align-items: center;

					.goods_value_prefix {
						font-size: 22rpx;
						padding-top: 8rpx;
					}

					.goods_value_text {
						transform: translateX(-4rpx);
					}
				}

				.discount_coupon_expire_time {
					font-size: 20rpx;
					margin-top: 20rpx;
				}
			}

			.left_price_wrap {
				width: var(--left-width);
				height: 100%;
				display: flex;
				flex-flow: column;
				align-items: center;
				justify-content: center;
				color: #fff;

				.name_text {
					font-size: 36rpx;
				}

				.name_desc {
					font-size: 18rpx;
					margin-top: 16rpx;
				}

				.discount_coupon_price {
					display: flex;
					align-items: flex-end;

					.price_value {
						font-size: 60rpx;
					}

					.price_prefix {
						font-size: 38rpx;
						padding-bottom: 4rpx;
					}
				}

				.discount_coupon_price_footer {
					display: flex;
					align-items: center;
					font-size: 20rpx;
					font-weight: normal;
					margin-top: 16rpx;
					transform: scale(.8);
				}
			}
		}

		.coupon_money {
			font-size: 30rpx;
			color: #fff;
			position: absolute;
			left: 350rpx;
		}

		.coupon_btn {
			position: absolute;
			color: #000;
			font-size: 24rpx;
			right: 38rpx;
		}

		.coupon_name {
			position: absolute;
			left: 54rpx;
			top: 78rpx;
			color: #fff;
			font-size: 34rpx;
		}

		.images-inner {
			position: absolute;
			left: 0;
			top: 0;
			right: 0;
			bottom: 0;

			.image {
				width: 100%;
				height: 100%;
			}
		}

		.coupon-line {
			position: absolute;
			left: 240rpx;
			top: 0;
			bottom: 0;
			width: 2rpx;
			box-sizing: border-box;
			padding: 16rpx 0;


			&::after,
			&::before {
				content: "";
				display: block;
				border-radius: 50%;
				overflow: hidden;
				width: 16rpx;
				height: 16rpx;
				background: #F6F7F9;
				position: absolute;
				left: 50%;
				box-shadow: 0 0 12rpx rgba(0, 0, 0, .12);
			}

			&::before {
				top: 0;
				transform: translate(-50%, -50%);
			}

			&::after {
				bottom: 0;
				transform: translate(-50%, 50%);
			}

			.line-inner {
				width: 100%;
				height: 100%;
				border: 1px dashed #FDBAAA;
			}
		}


		.coupon-info {
			margin-left: 68rpx;
			width: 230rpx;

			.coupon-time {
				height: 40rpx;
				font-size: 24rpx;
				color: rgba(0, 0, 0, 0.4);
				line-height: 40rpx;
			}

			.coupon-name {
				height: 48rpx;
				font-size: 28rpx;
				color: #1D2129;
				font-weight: bold;
				line-height: 48rpx;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}

		.price-wrap {
			width: 180rpx;

			.coupon-desc {
				height: 32rpx;
				font-size: 20rpx;
				color: rgba(0, 0, 0, 0.4);
				line-height: 32rpx;
				margin-top: 8rpx;
				text-align: center;
			}

			.money-wrap {
				display: flex;
				flex-flow: row nowrap;
				align-items: flex-end;
				justify-content: center;

				.prefix {
					font-size: 24rpx;
					font-weight: bold;
					color: #FC7655;
					padding-bottom: 8rpx;
				}

				.money-value {
					font-size: 58rpx;
					color: #FC7655;
					font-weight: bold;
				}
			}
		}

		.coupon-btn {
			width: 160rpx;
			height: 56rpx;
			background: linear-gradient(125deg, #FC7655 0%, #FFAC5E 100%);
			border-radius: 100rpx 100rpx 100rpx 100rpx;
			display: flex;
			flex-flow: row nowrap;
			align-items: center;
			justify-content: center;
			font-size: 26rpx;
			color: #FFFFFF;
			margin-left: auto;

			&.active {
				background: #ccc;
			}
		}
	}
</style>